package com.tianjin.design.flyweight;

import java.util.HashMap;
import java.util.Map;

public class PeopleFlyWeightFactory {

    // 预制几类共享对象
    private Map<String,PeopleFlyWeight> map = new HashMap<>();

    public PeopleFlyWeightFactory(){
        map.put("china-east", new ChineseFlyWeight());
        map.put("china-west", new ChineseFlyWeight());
        map.put("china-north", new ChineseFlyWeight());
        map.put("china-south", new ChineseFlyWeight());

        map.put("american-east", new AmericanFlyWeight());
        map.put("american-west", new AmericanFlyWeight());
    }

    public PeopleFlyWeight getPeopleFlyWeight(String key){

        if (!map.containsKey(key)) {
            if (key.startsWith("china")) {
                map.put(key, new ChineseFlyWeight());
            } else if (key.startsWith("american")) {
                map.put(key, new AmericanFlyWeight());
            }
        }

        return map.get(key);
    }

}
